﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="SqlAzureManagementClient.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagement
{
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;
    using System.Xml;

    using AzureManagement.SqlAzureCommands;
    using AzureManagement.SqlAzureDataContracts;

    using Common;

    using RestClient;

    public class SqlAzureManagementClient : AzureRestServiceClientBase, ISqlAzureManagementClient
    {
        public SqlAzureManagementClient(IRestServiceContext serviceContext)
            : base(serviceContext)
        {
        }

        public async Task<string> CreateSqlAzureServer(string adminLogin, string adminPassword, string location, RequestOptions options = null)
        {
            var response = await this.ExecuteCommand(
                                                () => new CreateSqlAzureServerCommand(
                                                                this.ServiceContext as IAzureServiceContext, 
                                                                new CreateSqlAzureServer
                                                                {
                                                                    AdministratorLogin = adminLogin, 
                                                                    AdministratorLoginPassword = adminPassword, 
                                                                    Location = location
                                                                }),
                                                                options);

            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(streamReader.ReadToEnd());

                var serverNameElement = xmlDocument.GetElementsByTagName("ServerName").Item(0);
                if (serverNameElement != null)
                {
                    return serverNameElement.InnerText;
                }
            }

            return string.Empty;
        }

        public async Task<IList<SqlAzureServer>> ListSqlAzureServers(RequestOptions options = null)
        {
            var response = await this.ExecuteCommand(() => new ListSqlAzureServersCommand(this.ServiceContext as IAzureServiceContext), options);

            return XmlHelper.Deserialize<SqlAzureServers>(response).Items;
        }

        public async Task<string> DeleteSqlAzureServer(string serverName, RequestOptions options = null)
        {
            var response = await this.ExecuteCommand(() => new DeleteSqlAzureServerCommand(this.ServiceContext as IAzureServiceContext, serverName), options);

            return this.GetOperationId(response);
        }

        public async Task<string> CreateFirewallRule(
                                            string serverName, 
                                            string ruleName, 
                                            string startIpAddresss, 
                                            string endIpAddress, 
                                            RequestOptions options = null)
        {
            var response = await this.ExecuteCommand(
                                                () => new CreateFirewallRuleCommand(
                                                                this.ServiceContext as IAzureServiceContext, 
                                                                serverName, 
                                                                ruleName, 
                                                                new FirewallRule
                                                                {
                                                                    StartIpAddress = startIpAddresss, 
                                                                    EndIpAddress = endIpAddress
                                                                }), 
                                                                options);

            return this.GetOperationId(response);
        }

        public async Task<string> CreateFirewallRuleWithIpDetect(string serverName, string ruleName, RequestOptions options = null)
        {
            var response = await this.ExecuteCommand(() => new CreateFirewallRuleWithIpDetect(this.ServiceContext as IAzureServiceContext, serverName, ruleName), options);

            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(streamReader.ReadToEnd());

                var ipAddressElement = xmlDocument.GetElementsByTagName("IpAddress").Item(0);
                if (ipAddressElement != null)
                {
                    return ipAddressElement.InnerText;
                }
            }

            return string.Empty;
        }
    }
}